import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from '@/router/routes'
import store from '@/store'
Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  routes
})

const needLoginPage = ['/newTopic']

router.beforeEach((to, from, next) => {
  const user = sessionStorage.getItem('user') || store.state.user.user
  if (user) {
    if (to.path === '/login') {
      next({
        path: '/'
      })
    } else {
      next()
    }
  } else if (needLoginPage.includes(to.path)) {
    next({
      path: '/login'
    })
  } else {
    next()
  }
})

export default router
